# @Version        : 1.0
# @Update Time    : 2025/3/14 23:08
# @File           : start_app_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
import flet as ft
import math
import time


class SplashScreen(ft.Container):
    def __init__(self, on_end):
        super().__init__(
            expand=True,
            bgcolor="white",
            alignment=ft.alignment.center,
            content=ft.Stack(
                controls=[
                    self._build_center_logo(),
                    self._build_animated_elements(),
                    self._build_text_section(on_end)
                ]
            )
        )

    def _build_center_logo(self):
        return ft.Container(
            content=ft.Icon(ft.icons.SHOPPING_BASKET, size=60, color="#FF7043"),
            alignment=ft.alignment.center
        )

    def _build_animated_elements(self):
        self.fruits = ft.Stack()
        positions = [
            {"angle": 0, "icon": ft.icons.APPLE, "color": "#FF5252"},
            {"angle": 90, "icon": ft.icons.CAKE, "color": "#FFAB40"},
            {"angle": 180, "icon": ft.icons.BUBBLE_CHART, "color": "#536DFE"},
            {"angle": 270, "icon": ft.icons.COOKIE, "color": "#6D4C41"}
        ]

        for pos in positions:
            self.fruits.controls.append(
                ft.Container(
                    content=ft.Icon(pos["icon"], color=pos["color"], size=30),
                    left=150,
                    top=150,
                    opacity=0,
                    animate_opacity=1000,
                    animate_position=1000
                )
            )
        return self.fruits

    def _build_text_section(self, on_end):
        return ft.Column(
            controls=[
                ft.Container(
                    content=ft.Text("生鲜到家", size=40, color="#FF7043"),
                    opacity=0,
                    animate_opacity=1000
                ),
                ft.Container(
                    content=ft.ElevatedButton(
                        "立即体验",
                        icon=ft.icons.ARROW_FORWARD,
                        style=ft.ButtonStyle(
                            bgcolor=ft.colors.WHITE,
                            shape=ft.RoundedRectangleBorder(radius=10)
                        ),
                        on_click=lambda e: on_end()
                    ),
                    opacity=0,
                    animate_opacity=1000
                )
            ],
            horizontal_alignment=ft.CrossAxisAlignment.CENTER,
            spacing=20,
            top=300,
            left=0,
            right=0
        )

    def start_animation(self):
        # 第一阶段：元素飞散
        for i, item in enumerate(self.fruits.controls):
            angle = math.radians(i * 90)
            item.left = 150 + math.cos(angle) * 100
            item.top = 150 + math.sin(angle) * 100
            item.opacity = 1
            item.update()
            time.sleep(0.1)

        # 第二阶段：文字显示
        text_section = self.content.controls[-1]
        for control in text_section.controls:
            control.opacity = 1
            control.update()


def main(page: ft.Page):
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    def switch_to_main(e):
        page.controls.clear()
        page.add(ft.Text("主界面内容..."))  # 替换为实际主界面

    splash = SplashScreen(on_end=switch_to_main)
    page.add(splash)

    # 直接触发动画
    splash.start_animation()


if __name__ == "__main__":
    ft.app(target=main)